package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.service.ArticleService;
import com.heima.common.constants.BehaviorConstants;
import com.heima.model.article.dto.ArticleInfoDto;
import com.heima.model.article.dto.CollectionBehaviorDto;
import com.heima.model.article.pojos.ArticleVo;
import com.heima.model.behavior.ApUserBehaviorDto;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {
        //校验数据
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //获取用户id
        ApUser apUser = ApThreadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //获取用户id
        Integer userId = apUser.getId();

        //文章id
        Long articleId = dto.getArticleId();

        //作者id
        Long authorId = dto.getAuthorId();

        //创建vo对象
        ArticleVo article = new ArticleVo();

        //操作hash
        HashOperations hashOperations = redisTemplate.opsForHash();

        //是否点赞  hash结构
        String like = (String) hashOperations.get(userId.toString(), BehaviorConstants.LIKE_BEHAVIOR + articleId);
        LikesBehaviorDto likesBehaviorDto = JSON.parseObject(like, LikesBehaviorDto.class);
        if (likesBehaviorDto != null){
            article.setIslike(likesBehaviorDto.getType() == 0 && likesBehaviorDto.getOperation() == 0);
        }

        //是否喜欢
        Object unLike =
                hashOperations.get(userId.toString(),
                BehaviorConstants.UN_LIKE_BEHAVIOR + articleId);
        if (unLike != null) {
            article.setIsunlike(unLike.toString().equals("0"));
        }

        //是否收藏
        String follow = (String) hashOperations.get(userId.toString(), BehaviorConstants.COLLECTION_BEHAVIOR + articleId);
        if (follow != null){
            CollectionBehaviorDto behaviorDto = JSON.parseObject(follow, CollectionBehaviorDto.class);
            article.setIscollection(behaviorDto.getType() == 0 && behaviorDto.getOperation() == 0);
        }

        //是否关注
        Object isFollow = hashOperations.get(userId.toString(), BehaviorConstants.APUSER_FOLLOW_RELATION + authorId);
        if (isFollow != null) {
            article.setIsfollow(isFollow.toString().equals("0"));
        }

        return ResponseResult.okResult(article);
    }


}
